package com.qianchang.microfinance.view.credit.data;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述:
 * 修 改 人: zhoulai
 * 修改时间: 2017/8/11 17:46
 */


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.moblie.zmxy.antgroup.creditsdk.app.CreditApp;
import com.android.moblie.zmxy.antgroup.creditsdk.app.ICreditListener;
import com.dm.library.adapter.CommonAdapter;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.DMLog;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.DampScrollView;
import com.dm.library.widgets.element.DButton;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.AttachmentListBean;
import com.qianchang.microfinance.bean.CreditParamBean;
import com.qianchang.microfinance.bean.DataInfoBean;
import com.qianchang.microfinance.bean.FindUserBasicInfoBean;
import com.qianchang.microfinance.bean.LimitBean;
import com.qianchang.microfinance.bean.LoanParamsBean;
import com.qianchang.microfinance.bean.LoanType;
import com.qianchang.microfinance.bean.MineUserInfo;
import com.qianchang.microfinance.bean.TongDunBean;
import com.qianchang.microfinance.bean.UserLoginInfo;
import com.qianchang.microfinance.bean.event.AgreementTitleBean;
import com.qianchang.microfinance.bean.event.PhoneAuthEvent;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.utils.SpanUtils;
import com.qianchang.microfinance.view.base.ImageChoseActivity;
import com.qianchang.microfinance.view.home.cash.ApplyCashActivity;
import com.qianchang.microfinance.view.hsrecharge.HSReChargeAuthorizedActivity;
import com.qianchang.microfinance.view.register.AgreementActivity;
import com.qianchang.microfinance.view.register.AgreementApi;
import com.lzy.imagepicker.bean.ImageItem;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 描 述: 认证信息（金保贷
 * 修 改 人: jinbailiang
 */


public class ConfirmInfoJinbaoActivity extends ImageChoseActivity {

    public static final String CURRENT_ID = "current_id";

    public static final int BASIC_INFO = 100;
    public static final int MATERIAL_INFO = 200;


    @BindView(R.id.recycler_data_info)
    RecyclerView rvDataInfo;
    @BindView(R.id.tv_protocol)
    TextView tvProtocol;
    @BindView(R.id.ll_agreement)
    LinearLayout llAgreement;
    @BindView(R.id.ck_protocol)
    CheckBox ckProtocol;
    List<DataInfoBean> infoList = new ArrayList<>();
    @BindView(R.id.btn_submit)
    DButton btnSubmit;
    @BindView(R.id.tv_credits_value)
    DTextView tvQuota;
//    @BindView(R.id.content)
//    DampScrollView dampScrollView;
    @BindView(R.id.content)
    ScrollView dampScrollView;
    @BindView(R.id.refresh_help)
    SwipeRefreshLayout refreshLayout;

    private DataInfoAdapter mDataInfoAdapter;
    private FindUserBasicInfoBean basicInfoBean;
    private LoanParamsBean loanParamsBean;
    private String currentId;
    private String productType;
    private int basicInfoStatus = 1;
    private int materialStatus = 1;
    private ArrayList<AttachmentListBean> attachmentListBeen;
    private LimitBean limitBean;
    private MineUserInfo mineUserInfo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_data);
        ButterKnife.bind(this);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initView();
        initData();

    }

    @Override
    protected void onResume() {
        super.onResume();
        updateStatus();
        getUserInfo();

    }

    private boolean isConfirmlGoing = true;

    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.Certification_Center));
        mDataInfoAdapter = new DataInfoAdapter(this, infoList);
        rvDataInfo.setLayoutManager(new LinearLayoutManager(this));
        rvDataInfo.setAdapter(mDataInfoAdapter);
        mDataInfoAdapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if (null == basicInfoBean) {
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, getString(R.string.data_error));
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putSerializable(ConstantAppInfo.BASIC_INFO_BEAN, basicInfoBean);
                bundle.putBoolean("isConfirml",isConfirmlGoing);
                switch (position) {
                    case 0:
                        startActivity(ScanIdentityActivity.class, bundle);
                        break;
                    default:
                        clickOthers(position, bundle);
                        break;
                }

            }
        });

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                updateStatus();
                getUserInfo();
            }
        });

    }


    private boolean isPhoneAuthClick = false;

    private void clickOthers(int position, final Bundle bundle) {
        int isNewsQianpen = basicInfoBean.getIsNewQianpen();
        if(isNewsQianpen != 0){                                 //判断是否是钱盆新用户 0：是，1：否
            ToastUtil.getInstant().show(getApplicationContext(),"系统检测到您当前帐号不符号我们平台贷款要求！");
            return;
        }
        switch (position) {
            case 1:   //人脸识别

                if (0 != basicInfoBean.getIsIdCardCheckThrough()) {
                    ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_IdCardCheck));
                    return;
                }

                if (basicInfoBean.getFaceRecogniteResult() == 1){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "已认证，无需再认证！");
                }else{
                    startActivity(ScanFaceActivity.class, bundle);
                }
                //测试使用
//                startActivity(ScanFaceActivity.class, bundle);

                break;
            case 2:             //联系人

                if (0 != basicInfoBean.getIsIdCardCheckThrough()) {
                    ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_IdCardCheck));
                    return;
                }

                if (basicInfoBean.getFaceRecogniteResult() != 1){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, getString(R.string.msg_shoild_submit_FaceRecognite));
                    return;
                }
                Log.i("wmy",""+basicInfoBean.getIsContactInfo());
                if(basicInfoBean.getIsContactInfo() == 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,"已认证，无需再认证！");
                }else{
                    startActivity(ContractInfoActivity.class, bundle);
                }


                break;
            case 3:   //手机验证


                if(isEventLoginSus){        //判断手机验证返回
                    ToastUtil.getInstant().show(this, "手机认证中");
                    return;
                }

                if (0 != basicInfoBean.getIsIdCardCheckThrough()) {
                    ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_IdCardCheck));
                    return;
                }

                if (basicInfoBean.getFaceRecogniteResult() != 1){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, getString(R.string.msg_shoild_submit_FaceRecognite));
                    return;
                }

                if(basicInfoBean.getIsContactInfo() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,getString(R.string.msg_shoild_submit_Contact));
                    return;
                }

                if(basicInfoBean.getIsMobile() == 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,"已认证，无需再认证！");
                    return;
                }else{
                    //2018.1.20 新增自己代码 所有前期认证已经完成，可以进行手机认证
                    startActivity(PhoneAuthActivity.class,bundle);
                    isPhoneAuthClick = true;
                }

                break;

            case 4: //绑定银行卡
                if (0 != basicInfoBean.getIsIdCardCheckThrough()) {
                    ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_IdCardCheck));
                    return;
                }

                if (basicInfoBean.getFaceRecogniteResult() != 1){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, getString(R.string.msg_shoild_submit_FaceRecognite));
                    return;
                }

                if(basicInfoBean.getIsContactInfo() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,getString(R.string.msg_shoild_submit_Contact));
                    return;
                }

                if(isPhoneAuthClick){           //如果是点击过手机认证的直接走下一步

                }else {
                    if(basicInfoBean.getIsMobile() != 0){
                        ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,"请先将手机实名验证");
                        return;
                    }
                }


                if(basicInfoBean.getIsBankCardBind() == 0){             //已认证，无需再认证！
                    startActivity(BankCardDetailActivity.class, bundle);
                }else{
//                    isEventLoginSus = false;
                    startActivity(BindBankCardActivity.class, bundle);
                }

//                startActivity(BindBankCardActivity.class, bundle);          //测试使用

                //测试使用2018.1.9
//                startActivity(HSBindBankCardActivity.class);

                //测试使用
//                startActivity(BindBankCardActivity.class, bundle);

                //测试使用
//                startActivity(HSUnBindBankCardActivity.class);

                //测试使用
//                startActivity(HSupBankCardPhoneActivity.class);
//                startActivity(HSupBankCardPswActivity.class);

//                startActivity(HSReChargeAuthorizedActivity.class);

//                startActivity(HSReChargeActivity.class);

//                startActivity(HSGoldReChargeActivity.class);

//                startActivity(HSWithdrawActivity.class);

                break;

                /***************** 注释掉同盾认证
            case 5:                                 //同盾认证      2018.1.2新增

                if (0 != basicInfoBean.getIsIdCardCheckThrough()) {
                    ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_IdCardCheck));
                    return;
                }

                if (basicInfoBean.getFaceRecogniteResult() != 1){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, getString(R.string.msg_shoild_submit_FaceRecognite));
                    return;
                }

                if(basicInfoBean.getIsContactInfo() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,getString(R.string.msg_shoild_submit_Contact));
                    return;
                }

                if(basicInfoBean.getIsMobile() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,"请先将手机实名验证");
                    return;
                }

                if(basicInfoBean.getIsBankCardBind() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,getString(R.string.msg_shoild_submit_bind_bank_card));
                    return;
                }

                if (0 == basicInfoBean.getIsTongDunAuth()) {                       //是否是同盾认证，0已经认证，1未认证
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,"已认证，无需再认证！");
                } else {
                    withTheShieldCertification();
                }

                break;
*******************************/
            case 5:                                     //芝麻信用

                if (LoanType.JBD.equals(productType)) {

                    if (0 != basicInfoBean.getIsIdCardCheckThrough()) {
                        ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_IdCardCheck));
                        return;
                    }

                    if (basicInfoBean.getFaceRecogniteResult() != 1){
                        ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, getString(R.string.msg_shoild_submit_FaceRecognite));
                        return;
                    }

                    if(basicInfoBean.getIsContactInfo() != 0){
                        ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,getString(R.string.msg_shoild_submit_Contact));
                        return;
                    }

                    if(isPhoneAuthClick){

                    }else {
                        if(basicInfoBean.getIsMobile() != 0){
                            ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,"请先将手机实名验证");
                            return;
                        }
                    }

                    /**********  2018.2.8需求更改占时注释
                    if(basicInfoBean.getIsMobile() != 0){
                        ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,"请先将手机实名验证");
                        return;
                    }*********/

                    if(basicInfoBean.getIsBankCardBind() != 0){
                        ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,getString(R.string.msg_shoild_submit_bind_bank_card));
                        return;
                    }

                    if(basicInfoBean.getIsAccountAuth() !=0 ){           //是否是新网银行充值，提现和还款的授权 0是 1否
//                        isEventLoginSus = false;
                        Intent intent = new Intent();
                        intent.setClass(ConfirmInfoJinbaoActivity.this,HSReChargeAuthorizedActivity.class);
                        startActivity(intent);
                        return;
                    }


                    if (0 != basicInfoBean.getIsTongDunAuth()) {                       //是否是同盾认证，0已经认证，1未认证
//                        ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先同盾认证");
//                        isEventLoginSus = false;
                        startActivity(GoSesameAuthorizedActivity.class,bundle,true);
                        return;
                    }


                    if(basicInfoBean.getIsSesameAuth() == 0){
                        ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "已认证，无需再认证！");
                        return;
                    }else{
//                        isEventLoginSus = false;
                        startActivity(GoSesameAuthorizedActivity.class);
                    }

//                    getCreditParams();                //注释掉原来迪蒙芝麻的2018.1.4


                } else {
                    //基本信息
                    bundle.putString(CURRENT_ID, currentId);
                    bundle.putString(ConstantAppInfo.PROJECT_TYPE, productType);
                    if (LoanType.XSD.equals(productType)) {
                        startActivity(BasicStudentActivity.class, bundle);
                    } else {
                        startActivityForResult(BasicOwnerActivity.class, bundle, BASIC_INFO);
                    }
                }

                break;
            case 6:                             //2017.12.27 鸿盛科技新增工作证明

                if (0 != basicInfoBean.getIsIdCardCheckThrough()) {
                    ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_IdCardCheck));
                    return;
                }

                if (basicInfoBean.getFaceRecogniteResult() != 1){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, getString(R.string.msg_shoild_submit_FaceRecognite));
                    return;
                }

                if(basicInfoBean.getIsContactInfo() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,getString(R.string.msg_shoild_submit_Contact));
                    return;
                }

                if(basicInfoBean.getIsMobile() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,"请先将手机实名验证");
                    return;
                }

                if(basicInfoBean.getIsBankCardBind() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,getString(R.string.msg_shoild_submit_bind_bank_card));
                    return;
                }

                if (0 != basicInfoBean.getIsTongDunAuth()) {                       //是否是同盾认证，0已经认证，1未认证
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先同盾认证");
                    return;
                }

                if(basicInfoBean.getIsTongDunAuth() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先芝麻认证");
                    return;
                }
                if(basicInfoBean.getIsHandIdCardAuth() == 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "已认证，无需再认证！");
                    return;
                }
                  startActivity(HandIdentityCarActivity.class);
//                if(basicInfoBean.getIsWorkProof() == 0){
//                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "已认证，无需再认证！");
//                }else {
//                    startActivityForResult(WorkProofActivity.class, bundle, BASIC_INFO);
//                }

                break;
            case 7:                             //2017.12.27 迪蒙原来代码
//                if (null != attachmentListBeen) {
//                    bundle.putString(ConstantAppInfo.PROJECT_ID, currentId);
//                    bundle.putString(ConstantAppInfo.PROJECT_TYPE, productType);
//                    startActivityForResult(CompleteMaterialActivity.class, bundle, MATERIAL_INFO);
//                }
                if (0 != basicInfoBean.getIsIdCardCheckThrough()) {
                    ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_IdCardCheck));
                    return;
                }

                if (basicInfoBean.getFaceRecogniteResult() != 1){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, getString(R.string.msg_shoild_submit_FaceRecognite));
                    return;
                }

                if(basicInfoBean.getIsContactInfo() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,getString(R.string.msg_shoild_submit_Contact));
                    return;
                }

                if(basicInfoBean.getIsMobile() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,"请先将手机实名验证");
                    return;
                }

                if(basicInfoBean.getIsBankCardBind() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,getString(R.string.msg_shoild_submit_bind_bank_card));
                    return;
                }

                if (0 != basicInfoBean.getIsTongDunAuth()) {                       //是否是同盾认证，0已经认证，1未认证
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先同盾认证");
                    return;
                }

                if(basicInfoBean.getIsTongDunAuth() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先芝麻认证");
                    return;
                }
                if(basicInfoBean.getIsHandIdCardAuth() == 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先手持身份证认证");
                    return;
                }
                if(basicInfoBean.getIsEmailAuth() == 1){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "已认证，无需再认证！");
                    return;
                }
                startActivity(EmailAuthenticationActivity.class);
                break;
            case 8:                             //2017.12.27 迪蒙原来代码
                if (0 != basicInfoBean.getIsIdCardCheckThrough()) {
                    ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_IdCardCheck));
                    return;
                }

                if (basicInfoBean.getFaceRecogniteResult() != 1){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, getString(R.string.msg_shoild_submit_FaceRecognite));
                    return;
                }

                if(basicInfoBean.getIsContactInfo() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,getString(R.string.msg_shoild_submit_Contact));
                    return;
                }

                if(basicInfoBean.getIsMobile() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,"请先将手机实名验证");
                    return;
                }

                if(basicInfoBean.getIsBankCardBind() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,getString(R.string.msg_shoild_submit_bind_bank_card));
                    return;
                }

                if (0 != basicInfoBean.getIsTongDunAuth()) {                       //是否是同盾认证，0已经认证，1未认证
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先同盾认证");
                    return;
                }

                if(basicInfoBean.getIsTongDunAuth() != 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先芝麻认证");
                    return;
                }
                if(basicInfoBean.getIsHandIdCardAuth() == 0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先手持身份证认证");
                    return;
                }
                if(basicInfoBean.getIsEmailAuth()==0){
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先邮箱认证");
                    return;
                }

                startActivity(RechargeCertificationActivity.class);

                break;
        }

        mDataInfoAdapter.notifyDataSetChanged();            //2017.12.28 占时新加测试使用
    }


    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getBundle();
        if (bundle != null) {
            loanParamsBean = (LoanParamsBean) bundle.getSerializable(ApplyCashActivity.LOAN_PARAMS);
            if (null != loanParamsBean) {
                currentId = loanParamsBean.getCurrentId();
                productType = loanParamsBean.getProductType();
            } else {
                productType = bundle.getString(ConstantAppInfo.PROJECT_TYPE);
            }
        }
        initItemData();
        getMaterialList();
    }

    private void initItemData() {
        if (null != infoList) {
            infoList.clear();
        } else {
            infoList = new ArrayList<>();
        }
        DataInfoBean sfxx = new DataInfoBean(getString(R.string.info_identity), 1, getString(R.string.never_checked), false);
        DataInfoBean rlsb = new DataInfoBean(getString(R.string.info_face_identity), 1, getString(R.string.never_checked), false);
        DataInfoBean lxrxx = new DataInfoBean(getString(R.string.info_contract), 1, getString(R.string.never_submit), true);
        DataInfoBean sjrz = new DataInfoBean(getString(R.string.phone_certified), 1, getString(R.string.never_checked), false);                //2018.1.15鸿盛科技新增
        DataInfoBean gzxx = new DataInfoBean(getString(R.string.info_bank_card), 1, getString(R.string.never_checked), false);
        DataInfoBean tdzm = new DataInfoBean(getString(R.string.with_the_shield_certification), 1, getString(R.string.never_checked), false);              //2017.1.2 鸿盛科技新增同盾认证
        DataInfoBean scsfz = new DataInfoBean("手持身份证认证", 1, getString(R.string.never_checked), false);
        DataInfoBean yxrz = new DataInfoBean("邮箱认证", 1, getString(R.string.never_checked), false);
        DataInfoBean xd = new DataInfoBean("续贷担保认证", 1, getString(R.string.never_checked), false);
//        DataInfoBean gzzm = new DataInfoBean(getString(R.string.work_proof), 1, getString(R.string.never_checked), false);              //2017.12.27 鸿盛科技新增工作证明

        infoList.add(sfxx);
        infoList.add(rlsb);
        infoList.add(lxrxx);
        infoList.add(sjrz);
        infoList.add(gzxx);
//        infoList.add(tdzm);       2018.1.26注释

        if (LoanType.JBD.equals(productType)) {
            setTitleName(getString(R.string.Certification_Center));
            DataInfoBean zmxx = new DataInfoBean(getString(R.string.info_sesame_credit), 1, getString(R.string.never_checked), false);
            infoList.add(zmxx);
            llAgreement.setVisibility(View.GONE);
            JSONArray ja = new JSONArray();
            ja.put("7");   //个人征信查询授权书
            ja.put("10");  //SaaS智能风控服务授权书
            AgreementApi.getAgreementName(this, ja, new AgreementApi.AgreementNameCallBack() {
                @Override
                public void success(List<AgreementTitleBean> agreementName) {
                    if (null != agreementName && agreementName.size() >= 2) {
                        AgreementTitleBean agreementOne = agreementName.get(0);
                        AgreementTitleBean agreementTwo = agreementName.get(1);

                        tvProtocol.setMovementMethod(LinkMovementMethod.getInstance());
                        tvProtocol.setHighlightColor(getResources().getColor(android.R.color.transparent));
                        SpanUtils spanUtils = new SpanUtils();
                        spanUtils.append(getString(R.string.Label_reading)).setForegroundColor(getResources().getColor(R.color.common_black_color_6)).append("《" + agreementOne.getAgreementName() + "》")
                                .setClickSpan(new ClickableSpan() {
                                    @Override
                                    public void onClick(View widget) {
                                        Bundle bundle = new Bundle();
                                        bundle.putSerializable(ConstantAppInfo.AGREEMENT_ID, "7");
                                        startActivity(AgreementActivity.class, bundle);
                                    }

                                    @Override
                                    public void updateDrawState(TextPaint ds) {
                                        ds.setColor(getResources().getColor(R.color.common_blue_color_light));
                                        ds.setUnderlineText(false);
                                    }

                                })
                                .append("《" + agreementTwo.getAgreementName() + "》")
                                .setClickSpan(new ClickableSpan() {
                                    @Override
                                    public void updateDrawState(TextPaint ds) {
                                        ds.setColor(getResources().getColor(R.color.common_blue_color_light));
                                        ds.setUnderlineText(false);
                                    }

                                    @Override
                                    public void onClick(View widget) {
                                        Bundle bundle = new Bundle();
                                        bundle.putSerializable(ConstantAppInfo.AGREEMENT_ID, "10");
                                        startActivity(AgreementActivity.class, bundle);
                                    }
                                });
                        tvProtocol.setText(spanUtils.create());

                    }
                }
            });
        } else {
            setTitleName(getString(R.string.recoglizeData));
            DataInfoBean jbxx = new DataInfoBean(getString(R.string.info_basic), 1, getString(R.string.never_submit), true);
            infoList.add(jbxx);
            llAgreement.setVisibility(View.GONE);
        }
    infoList.add(scsfz);
        infoList.add(yxrz);
        infoList.add(xd);
//        infoList.add(gzzm);             //2017.12.27 鸿盛科技新增工作证明

        mDataInfoAdapter.notifyDataSetChanged();
    }

    /**
     * 获取资料认证状态
     */
    private void updateStatus() {
        show();
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.FIND_USER_BASIC_INFO, params, new RequestCallBack<FindUserBasicInfoBean>() {
            @Override
            public void onSuccess(JsonResult<FindUserBasicInfoBean> response) {
                refreshLayout.setRefreshing(false);
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    basicInfoBean = response.getObject();
                    if (null != basicInfoBean) {
                        refreshUi();
                    }
                } else {
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, response.getDescription());
                }
            }


            @Override
            public void onFailure(String msg) {
                refreshLayout.setRefreshing(false);
                dismiss();
                ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }

    /**
     * 2018.1.18鸿胜科技新增加
     */
    public void getUserInfo() {
        HttpParams params = new HttpParams();
        show();
        DMOKHttpUtils.getInstance().post(getApplicationContext(), DMConstant.ApiURL.FIND_USER_INFO, params, new RequestCallBack<MineUserInfo>() {

            @Override
            public void onSuccess(JsonResult<MineUserInfo> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    mineUserInfo = response.getObject();
                    refreshViewMineInfo(mineUserInfo);

                } else {
                    if (!"300001".equals(response.getCode())) {
                        if (!"300084".equals(response.getCode())) {
                            ToastUtil.getInstant().show(getApplicationContext(), response.getDescription());
                        }
                    } else {
                        DMApplication.getInstance().setUserLoginInfo(null);
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(getApplicationContext(), msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }




    private void getMaterialList() {
        HttpParams params = new HttpParams();
        show();
        params.put("productType", productType);
        params.put("projectId", currentId);
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.FIND_PRODUCT_DATA_LIST, params, new RequestCallBack<List<AttachmentListBean>>() {
            @Override
            public void onSuccess(JsonResult<List<AttachmentListBean>> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    attachmentListBeen = (ArrayList<AttachmentListBean>) response.getObject();
                    if (!attachmentListBeen.isEmpty()) {
                        DataInfoBean fjxx = new DataInfoBean(getString(R.string.info_material), 1, getString(R.string.never_submit), true);
                        infoList.add(fjxx);
                    }
                } else {
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
            }

        });
    }

    private void refreshUi() {
        mDataInfoAdapter.setFindUserBasicInfoBean(basicInfoBean);
        int isFirstAuth = basicInfoBean.getIsFirstAuth();
        int isIdCardCheckThrough = basicInfoBean.getIsIdCardCheckThrough();
        int faceRecogniteResult = basicInfoBean.getFaceRecogniteResult();
        int isMobile = basicInfoBean.getIsMobile();
        int isContactInfo = basicInfoBean.getIsContactInfo();
        int isBankCardBind = basicInfoBean.getIsBankCardBind();
        int isSesameAuth = basicInfoBean.getIsSesameAuth();
        int isTongDunAuth = basicInfoBean.getIsTongDunAuth();     //占时注释同盾认证内容
        int isWorkProof = basicInfoBean.getIsWorkProof();

        infoList.get(0).setStatus(isIdCardCheckThrough);
        if (faceRecogniteResult == 0) {
            infoList.get(1).setStatus(1);
        } else {
            infoList.get(1).setStatus(0);
        }
        infoList.get(2).setStatus(isContactInfo);
        infoList.get(3).setStatus(isMobile);
        infoList.get(4).setStatus(isBankCardBind);
        if(basicInfoBean.getIsEmailAuth()==0){
            infoList.get(7).setStatus(3);
        }
        if(basicInfoBean.getIsEmailAuth()==1){
            infoList.get(7).setStatus(0);
        }
        if(basicInfoBean.getIsHandIdCardAuth()==0){
            infoList.get(6).setStatus(1);
        } if(basicInfoBean.getIsHandIdCardAuth()==1){
            infoList.get(6).setStatus(4);
        } if(basicInfoBean.getIsHandIdCardAuth()==2){
            infoList.get(6).setStatus(0);
        }
//        infoList.get(5).setStatus(isTongDunAuth);             //占时注释同盾认证内容

        if (LoanType.JBD.equals(productType)) {
            if(isTongDunAuth == 0 && isSesameAuth == 0){            //同盾认证 0 是，1 否    //芝麻验证通过   0 是，1 否
                infoList.get(5).setStatus(0);
            }else{
                infoList.get(5).setStatus(1);                       //同盾认证 0 是，1 否    //芝麻验证通过   0 是，1 否
            }
        } else {
            if (LoanType.XSD.equals(productType)) {
                basicInfoStatus = basicInfoBean.getIsBasicInfo();
                if (1 != basicInfoStatus) {
                    basicInfoStatus = 0;
                }
                infoList.get(5).setStatus(basicInfoStatus);
            }
        }

//        infoList.get(6).setStatus(isWorkProof);       //需求变更，注释掉工作证明2018.2.8

        if(isFirstAuth == 0 || isFirstAuth == 3){           //是否认证过一次 0是 1否，2正在获取额度 3有额度
            btnSubmit.setVisibility(View.GONE);
        }else {
            btnSubmit.setVisibility(View.VISIBLE);
        }

        mDataInfoAdapter.notifyDataSetChanged();

        setPhoneAuthUi();

    }


    private void setPhoneAuthUi() {
        if (isEventLoginSus) {
            if (infoList != null && infoList.size() >= 4) {
                DataInfoBean sjrz = infoList.get(3);
                sjrz.setTitle("手机认证");
                sjrz.setStatus(2);
                sjrz.setStrStatus("认证中");
                sjrz.setNeedCommit(false);
                if (mDataInfoAdapter != null) {
                    mDataInfoAdapter.notifyDataSetChanged();
                }
            }
        }
    }


    @OnClick(R.id.btn_submit)
    public void onViewClicked() {
        if (checkClick(R.id.btn_submit)) {
//            commit();                     //2018.1.23 注释掉迪蒙原来代码

            getQuota();         //鸿胜科技新增 评估额度

        }
    }


    //认证成功所有评估额度
    private void getQuota(){

        if (0 != basicInfoBean.getIsIdCardCheckThrough()) {
            ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_IdCardCheck));
            return;
        }

        if (basicInfoBean.getFaceRecogniteResult() != 1){
            ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, getString(R.string.msg_shoild_submit_FaceRecognite));
            return;
        }

        if(basicInfoBean.getIsContactInfo() != 0){
            ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,getString(R.string.msg_shoild_submit_Contact));
            return;
        }

        if(basicInfoBean.getIsMobile() != 0){
            ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,"请先将手机实名验证");
            return;
        }

        if(basicInfoBean.getIsBankCardBind() != 0){
            ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this,getString(R.string.msg_shoild_submit_bind_bank_card));
            return;
        }

        if (0 != basicInfoBean.getIsTongDunAuth()) {                       //是否是同盾认证，0已经认证，1未认证
            ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先芝麻认证");      //因为同盾未认证要进去芝麻认证授权页面认证同盾认证，所以提示语为先芝麻认证
            return;
        }

        if(basicInfoBean.getIsTongDunAuth() != 0){
            ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先芝麻认证");
            return;
        }
        if(basicInfoBean.getIsHandIdCardAuth() == 0){
            ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先手持身份证认证");
            return;
        }
        if(basicInfoBean.getIsEmailAuth() != 1){
            ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先邮箱认证");
            return;
        }

//        if(basicInfoBean.getIsWorkProof() != 0){
//            ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先工作证明认证");
//            return;
//        }

        show();
        UserLoginInfo userLoginInfo = DMApplication.getInstance().getmUserLoginInfo();
        String usid = userLoginInfo.getUserId();
        HttpParams params = new HttpParams();
//        Log.i("TAG","userId = "+usid);
//        params.put("userId",usid);
//        params.put("userId",usid);
        DMOKHttpUtils.getInstance().post(ConfirmInfoJinbaoActivity.this, DMConstant.ApiURL.FIND_USERCREDITS, params, new RequestCallBack<LimitBean>() {

            @Override
            public void onSuccess(JsonResult<LimitBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    limitBean = response.getObject();
                    Log.i("TAG","limitBean.getLimit() = "+limitBean.getLimit());
                    refreshView(limitBean);
                    btnSubmit.setVisibility(View.GONE);
                }else {
                    Log.i("TAG","onSuccess ");
                    refreshView(null);
                }
            }

            @Override
            public void onFailure(String msg) {
                Log.i("TAG","onFailure = "+msg);
                refreshView(null);
                dismiss();
            }

            @Override
            public void onConnectFailure(String msg) {
                Log.i("TAG","onConnectFailure = "+msg);
                refreshView(null);
                dismiss();
            }
        });
    }

    private void refreshView(LimitBean limitBean){
        dampScrollView.scrollTo(0,0);
        if(limitBean != null){
            tvQuota.setText(String.valueOf(limitBean.getLimit()));
            String limitStr = String.valueOf(limitBean.getLimit());
            Log.i("wmy","limitStr = "+limitStr);
            if(limitStr != null && !limitStr.equals("")){
                btnSubmit.setVisibility(View.GONE);
            }else {
                btnSubmit.setVisibility(View.VISIBLE);
            }
        }else{
            tvQuota.setText("0.00");
            btnSubmit.setVisibility(View.VISIBLE);
        }

    }


    private void refreshViewMineInfo(MineUserInfo mineUserInfo){
        dampScrollView.scrollTo(0,0);
        if(mineUserInfo != null){
            String strLoa = String.valueOf(mineUserInfo.getLoanLimit());
            if(strLoa == null || strLoa.equals("") || strLoa.equals("null")){
                tvQuota.setText("0.00");       //授信总额度
            }else {
                tvQuota.setText(String.valueOf(mineUserInfo.getLoanLimit()));       //授信总额度
            }
        }else{
            tvQuota.setText("0.00");
        }
    }


    //尚无额度【测测信用额度】 提交接口
    private void commit() {

        /******/
        if (null == basicInfoBean) {
            ToastUtil.getInstant().show(this, getString(R.string.data_error));
            return;
        }
        if (0 != basicInfoBean.getIsIdCardCheckThrough()) {
            ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_IdCardCheck));
            return;
        }
        if (1 != basicInfoBean.getFaceRecogniteResult()) {
            ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_FaceRecognite));
            return;
        }
        if (0 != basicInfoBean.getIsContactInfo()) {
            ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_Contact));
            return;
        }
        if (0 != basicInfoBean.getIsBankCardBind()) {
            ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_bind_bank_card));
            return;
        }

        if (LoanType.JBD.equals(productType)) {

            if (0 != basicInfoBean.getIsSesameAuth()) {
                ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_SesameAuth));
                return;
            }

            if (!ckProtocol.isChecked()) {
                ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, getString(R.string.msg_read_loan_agreement));
                return;
            }

            show();
            HttpParams params = new HttpParams();

            DMOKHttpUtils.getInstance().post(ConfirmInfoJinbaoActivity.this, DMConstant.ApiURL.TEST_AMOUNT, params, new RequestCallBack<String>() {
                @Override
                public void onSuccess(JsonResult<String> response) {
                    dismiss();
                    if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
//                        startActivity(ApplyCashActivity.class, true);
                        finish();
                    } else {
                        ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, response.getDescription());
                    }
                }

                @Override
                public void onFailure(String msg) {
                    dismiss();
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, msg);//
                }

                @Override
                public void onConnectFailure(String msg) {
                }
            });
        } else {
            if (0 != basicInfoStatus) {
                ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "请先提交基本信息");//
                return;
            }

            if (null != attachmentListBeen && !attachmentListBeen.isEmpty()) {
                if (0 != materialStatus) {
                    ToastUtil.getInstant().show(this, getString(R.string.msg_shoild_submit_material_info));
                    return;
                }
            }
            show();
            HttpParams params = new HttpParams();
            params.put("applyAmount", loanParamsBean.getApplyAmount());
            params.put("deadline", loanParamsBean.getDeadline());
            params.put("deadlinekey", loanParamsBean.getDeadlinekey());
            params.put("lastProductUpdateTime", loanParamsBean.getLastProductUpdateTime());
            params.put("productId", loanParamsBean.getProductId());
            params.put("projectId", loanParamsBean.getCurrentId());
            params.put("productType", loanParamsBean.getProductType());
            params.put("repaymentType", loanParamsBean.getRepaymentType());
            params.put("serviceFeeRate", loanParamsBean.getServiceFeeRate());
            params.put("serviceFeeSetting", loanParamsBean.getServiceFeeSetting());
            params.put("unit", loanParamsBean.getUnit());

            DMOKHttpUtils.getInstance().post(ConfirmInfoJinbaoActivity.this, DMConstant.Config.BASE_USER + "/project/" + LoanType.getNameByType(productType) + DMConstant.Config.V + "/insert.do", params, new RequestCallBack<String>() {
                @Override
                public void onSuccess(JsonResult<String> response) {
                    dismiss();
                    if (response.isReqDataSuccess()) {
                        ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, "申请成功");
                        finish();
                    } else {
                        ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, response.getDescription());
                    }
                }

                @Override
                public void onFailure(String msg) {
                    dismiss();
                    ToastUtil.getInstant().show(ConfirmInfoJinbaoActivity.this, msg);
                }

                @Override
                public void onConnectFailure(String msg) {
                }

            });
        }
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        CreditApp.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (!LoanType.JBD.equals(productType)) {
                if (requestCode == BASIC_INFO) {
                    basicInfoStatus = 0;
                    infoList.get(4).setStatus(basicInfoStatus);
                } else if (requestCode == MATERIAL_INFO) {
                    materialStatus = 0;
                    infoList.get(5).setStatus(materialStatus);
                }
                mDataInfoAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onPhotoResult(ArrayList<ImageItem> imageItems, int flag) {
//        if (null != imageItems) {
//            String imageUrl = uploadFileBean.getUrl();
//            updateUserPhoto(imageUrl, imagePath);
//        }
//        Bundle bundle = new Bundle();
//        bundle.putString(IDENTIFY_PATH, imageItems.get(0).path);
//        startActivity(FaceVideoRecordActivity.class, bundle);//未进行实名认证，点击 人脸识别、银行卡信息，提示“请先完成实名认证”；
    }


    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }




    /***
     * 从手机验证页面返回的EVenet 回调
     * @param phoneAuthEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getStringStatus(PhoneAuthEvent phoneAuthEvent){
        PhoneAuthEvent.PhoneAuthStatue phoneAuthStatue = phoneAuthEvent.getPhoneAuthStatue();
        switch (phoneAuthStatue){
            case PHONEAUTH_INITIALIZE:              //"未初始化";
                DMLog.e("AuthEvent 未初始化");
                isEventLoginSus =false;
                break;
            case PHONEAUTH_START_CRAWLING:          //抓取开始
                DMLog.e("AuthEvent 抓取开始");

                break;
            case PHONEAUTH_LOGIN_SUS:               //登录成功  验证中
                DMLog.e("AuthEvent 登录成功  验证中");
                if(infoList != null && infoList.size() >= 4){
                    isEventLoginSus = true;                                 //从手机验证页面 Event 返回判断
//                    DataInfoBean sjrz = infoList.get(3);
//                    DataInfoBean msjrz = new DataInfoBean("手机认证", 1, "认证中", false);
//                    infoList.set(3,msjrz);
//                    if( mDataInfoAdapter != null){
//                        mDataInfoAdapter.notifyDataSetChanged();
//                    }

                    if (null != infoList) {
                        infoList.clear();
                    } else {
                        infoList = new ArrayList<>();
                    }
                    DataInfoBean sfxx = new DataInfoBean(getString(R.string.info_identity), 0, getString(R.string.has_checked), false);
                    DataInfoBean rlsb = new DataInfoBean(getString(R.string.info_face_identity), 0, getString(R.string.has_checked), false);
                    DataInfoBean lxrxx = new DataInfoBean(getString(R.string.info_contract), 0, getString(R.string.has_checked), true);
                    DataInfoBean sjrz = new DataInfoBean(getString(R.string.phone_certified), 2, getString(R.string.never_checked_certification), false);                //2018.1.15鸿盛科技新增
                    DataInfoBean gzxx = new DataInfoBean(getString(R.string.info_bank_card), 1, getString(R.string.never_checked), false);
                    DataInfoBean zmxx = new DataInfoBean(getString(R.string.info_sesame_credit), 1, getString(R.string.never_checked), false);
                    DataInfoBean scsfz = new DataInfoBean("手持身份证认证", 1, getString(R.string.never_checked), false);
                    DataInfoBean yxrz = new DataInfoBean("邮箱认证", 1, getString(R.string.never_checked), false);
                    DataInfoBean xd = new DataInfoBean("续贷担保认证", 1, getString(R.string.never_checked), false);
                    infoList.add(sfxx);
                    infoList.add(rlsb);
                    infoList.add(lxrxx);
                    infoList.add(sjrz);
                    infoList.add(gzxx);
                    infoList.add(zmxx);
                    infoList.add(scsfz);
                    infoList.add(yxrz);
                    infoList.add(xd);
                    mDataInfoAdapter.notifyDataSetChanged();

                }

                break;
            case PHONEAUTH_CRAWLING_SUS:             //抓取成功 验证成功
                DMLog.e("AuthEvent 抓取成功 验证成功");
                isPhoneAuthClick = false;       //是否点击过进去手机验证页面
                isEventLoginSus = false;
                updateStatus();
                getUserInfo();

                break;
            case PHONEAUTH_FAIL:                     //失败
                DMLog.e("AuthEvent 失败");

                if(infoList != null && infoList.size() >= 4){
                    isEventLoginSus = false;
                    DataInfoBean sjrz = infoList.get(3);
                    sjrz.setTitle("手机认证");
                    sjrz.setStatus(1);
                    sjrz.setStrStatus("认证失败");
                    sjrz.setNeedCommit(false);
                    if( mDataInfoAdapter != null){
                        mDataInfoAdapter.notifyDataSetChanged();
                    }
                }

                break;
            case PHONEAUTH_BACK_UI:                  //用户返回界面
                DMLog.e("AuthEvent 用户返回界面");
                isEventLoginSus = false;
                break;
            case PHONEAUTH_OPEN_ALIPAY:              //打开登陆页面,支付宝和淘宝的登陆页面
                DMLog.e("AuthEvent 打开登陆页面,支付宝和淘宝的登陆页面");
                isEventLoginSus = false;
                break;
            default:
                isEventLoginSus = false;
                break;
        }
    }


}
